package cn.lystudio.rohmtimeopwide_background_v11.controller.appApi;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohmtimeopwide_background_v11.service.FromRohmCapacityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app-api/fromRohmCapa")
@CrossOrigin
@SaIgnore
@Slf4j
public class AppFromRohmCapacityController {
	private FromRohmCapacityService fromRohmCapacityService;
	@Autowired
	public void setFromRohmCapacityService(FromRohmCapacityService fromRohmCapacityService) {
		this.fromRohmCapacityService = fromRohmCapacityService;
	}

	@GetMapping("/getRohmCapaList")
	public SaResult getRohmCapaList(@RequestParam("recordTime")String recordTime,@RequestParam("machineNo")String machineNo) {
		if(StrUtil.isEmpty(machineNo)) return SaResult.error("设备号参数异常");
		if(StrUtil.isEmpty(recordTime) || (!StrUtil.contains(recordTime," "))) return SaResult.error("参数异常");
		List<String> splitStr = StrUtil.split(recordTime, " ");
		if(splitStr.size() != 2) return SaResult.error("参数异常2");
		String recordTimeDT=splitStr.get(0);
		String dayOrNight=splitStr.get(1);
		if(StrUtil.contains(dayOrNight,"1")){
			String startTimeDT= recordTimeDT + " 07:00:00";
			String endTimeDT= recordTimeDT + " 19:00:00";
			return SaResult.data(fromRohmCapacityService.getRohmCapaList(startTimeDT, endTimeDT,machineNo));
		}else if(StrUtil.contains(dayOrNight,"2")){
			String startTimeDT= recordTimeDT + " 19:00:00";
			DateTime parseDate = DateUtil.parse(startTimeDT);
			DateTime tomorrowDay = DateUtil.offsetDay(parseDate, 1);
			String formatTomorrowDay = DateUtil.format(tomorrowDay, "yyyy-MM-dd");
			String endTimeDT= formatTomorrowDay + " 07:00:00";
			return SaResult.data(fromRohmCapacityService.getRohmCapaList(startTimeDT, endTimeDT,machineNo));
		}
		return SaResult.error("参数异常3");
	}
}
